package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.StateBean;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.db.DaoManager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/9/28.
 * 注册审核结果页面
 */

public class RegisterResultActivity extends BaseActivity {

    /**
     * apply：等待审核
     * normal：账户正常
     * freeze：账户冻结
     * refuse：审核失败
     */
    public static final int apply = 0;
    public static final int normal = 1;
    public static final int freeze = 2;
    public static final int refuse = 3;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.tv_reapply)
    View reapply;
    @BindView(R.id.tv_wait_hint)
    TextView tvWaitHint;


    @Override
    protected int setLayout() {
        return R.layout.activity_register_result;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        //放在onStart里查询状态，方便应用进入在后台期间，状态发生了改变，所以再进行查询
        getInfo();
    }

    private void getInfo() {
        ApiCreator.getApi().getUserStatus()
                .compose(RxTransformer.<StateBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<StateBean>() {
                    @Override
                    protected void onSuccess(StateBean data) {
                        initData(data.getState());
                    }
                });
    }

    private void initData(int status) {
        switch (status) {
            case apply:
                headline.setText("等待认证");
                ivType.setImageDrawable(getResources().getDrawable(R.drawable.ic_wait_gray));
                tvName.setText("等待审核");
                tvHint.setText("（预计审核时间2小时）");
                tvInfo.setText("认证成功后，您将受到来自E单配的短信、客服电话通知，请您保持手机通畅，并配合核查。");
                tvWaitHint.setVisibility(View.VISIBLE);
                tvWaitHint.setText("如有疑问请拨打客服电话：" + DaoManager.getInstance().getUserBean().getCompanyServerPhone());
                break;
            case refuse:
                headline.setText("认证失败");
                ivType.setImageDrawable(getResources().getDrawable(R.drawable.ic_fail));
                tvName.setText("审核失败");
                tvName.setTextColor(getResources().getColor(R.color.red));
                tvHint.setText("（信息有误，请重新申请）");
                tvInfo.setText("请您检查您所提交审核的信息是否有错误，检查结束后，请再次如实填写您的审核信息。");
                reapply.setVisibility(View.VISIBLE);
                break;
            case normal:
                startActivity(MainActivity.class);
                finish();
                break;
            case freeze:
                //账户冻结
                headline.setText("账户冻结");
                ivType.setImageDrawable(getResources().getDrawable(R.drawable.ic_fail));
                tvName.setText("账户冻结");
                tvName.setTextColor(getResources().getColor(R.color.red));
                headline.setText("等待认证");
                tvHint.setText("（账户冻结中，暂停使用）");
                tvInfo.setText("因为您的操作不当，此账户暂被冻结，有什么疑问可以拨打我们的客服电话：" + DaoManager.getInstance().getUserBean().getCompanyServerPhone());
                break;
            default:
                break;
        }

    }


    /**
     * 点击重新审核
     */
    @OnClick(R.id.tv_reapply)
    void onReapply() {
        ApiCreator.getApi().reapply()
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        //清除本地实名认证状态
                        DaoManager.getInstance().setIsReal(false);
                        //清除本地实名认证姓名
                        DaoManager.getInstance().setRealName("");
                        //清除本地公司信息
                        DaoManager.getInstance().setCompanyName("");
                        //进入角色选择页
                        startActivity(RoleChoiceActivity.class);
                        finish();
                    }
                });
    }


    /**
     * 点击返回
     */
    @OnClick(R.id.iv_headline_back)
    void close() {
        //返回登陆页
        startActivity(LoginActivity.class);
        DaoManager.getInstance().logout();
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            startActivity(LoginActivity.class);
//            DaoManager.getInstance().logout();
//            finish();
            close();
        }
        return false;
    }

}
